import json
import requests

class send:
    def __init__(self, PUSH_PLUS_TOKEN, topic):
        self.PUSH_PLUS_TOKEN = PUSH_PLUS_TOKEN
        self.topic = topic

    def pushplus_bot(self, content):
        try:
            print("\n")
            if not self.PUSH_PLUS_TOKEN:
                print("PUSHPLUS服务的token未设置!!\n取消推送")
                return
            print("PUSHPLUS服务启动")
            url = 'http://www.pushplus.plus/send'
            data = {
                "token": self.PUSH_PLUS_TOKEN,
                "content": content,
                "topic": self.topic
            }
            body = json.dumps(data).encode(encoding='utf-8')
            headers = {'Content-Type': 'application/json'}
            response = requests.post(url=url, data=body, headers=headers).json()
            if response['code'] == 200:
                print('推送成功！')
            else:
                print('推送失败！')
        except Exception as e:
            print(e)
